const yapi = require('../yapi.js');
const baseModel = require('./base.js');

class interfaceMethodModel extends baseModel {
    getName() {
        return 'interfaceMethod';
    }

    getSchema() {
        return {
            cname: {type: String, required: true},
            mname: {type: String, required: true},
            inname: {type: String, required: true},
            outname: {type: String, required: true},
            ctime: Number,
            etime: Number,
            inbody: [
                {
                    name: {type: String, required: true},
                    name_cn: String,
                    field_type: {type: String, required: true, enum: ['String', 'Object', 'Number', 'Boolean', 'List', 'Integer']},
                    length: Number,
                    default: String,
                    desc: String
                }
            ],
            outbody: [
                {
                    name: {type: String, required: true},
                    name_cn: String,
                    field_type: {type: String, required: true, enum: ['String', 'Object', 'Number', 'Boolean', 'List', 'Integer']},
                    length: Number,
                    default: String,
                    desc: String
                }
            ],
            cuserid: {type: Number, required: true},
            euserid: {type: Number, required: true},
            status: {
                type: Number,
                required: true,
                enum: [1, 2, 3, 4]
            },
            onlinedate: String,
            batch: {type: Number, default: 1},
            infid: {type: Number, required: true},
            desc: String
        };
    }

    save(data) {
        let m = new this.model(data);
        return m.save();
    }

    get(id) {
        return this.model
            .findOne({
                _id: id
            })
            .exec();
    }

    getBaseinfo(id) {
        return this.model
            .findOne({
                _id: id
            })
            .select('cname mname infid inname outname cuserid status ')
            .exec();
    }

    getByInf(infid, method, select) {
        select =
            select ||
            'cname mname ctime etime onlinedate  infid';
        return this.model
            .find({
                infid: infid,
                method: method
            })
            .select(select)
            .exec();
    }

    checkRepeat(infid, mname) {
        return this.model.countDocuments({
            infid: infid,
            mname: mname
        });
    }

    countByInfId(infid) {
        return this.model.countDocuments({
            infid: infid
        });
    }

    list(infid, select) {
        select =
            select || '_id cname mname infid inname outname status ctime onlinedate';
        return this.model
            .find({
                infid: infid
            })
            .select(select)
            .sort({mname: 1 })
            .exec();
    }

    listByInfId(infid) {
        return this.model
            .find({
                infid: infid
            })
            .sort({ mname: 1 })
            .exec();
    }

    listWithPage(infid, page, limit) {
        page = parseInt(page);
        limit = parseInt(limit);
        return this.model
            .find({
                infid: infid
            })
            .sort({ mname: 1 })
            .skip((page - 1) * limit)
            .limit(limit)
            .select(
                '_id cname mname inname inbody outname outbody ctime cuserid etime euserid status onlinedate batch infid'
            )
            .exec();
    }


    del(id) {
        return this.model.remove({
            _id: id
        });
    }

    delByInfId(infid) {
        return this.model.remove({
            infid: infid
        });
    }

    up(id, data) {
        data.etime = yapi.commons.time();
        return this.model.update(
            {
                _id: id
            },
            data,
            { runValidators: true }
        );
    }

    listCount(option) {
        return this.model.countDocuments(option);
    }

    upIndex(id, index) {
        return this.model.update(
            {
                _id: id
            },
            {
                index: index
            }
        );
    }

    search(keyword) {
        return this.model
            .find({
                title: new RegExp(keyword, 'ig')
            })
            .limit(10);
    }
}
module.exports = interfaceMethodModel;
